package cn.tedu.management.portal.service.impl;

import cn.tedu.management.portal.dto.StaffDTO;
import cn.tedu.management.portal.ex.*;
import cn.tedu.management.portal.mapper.DepartmentMapper;
import cn.tedu.management.portal.model.Department;
import cn.tedu.management.portal.model.Staff;
import cn.tedu.management.portal.mapper.StaffMapper;
import cn.tedu.management.portal.service.IStaffService;
import cn.tedu.management.portal.util.PasswordUtils;
import cn.tedu.management.portal.vo.StaffLoginVO;
import cn.tedu.management.portal.vo.StaffUpdateVO;
import cn.tedu.management.portal.vo.StaffVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    StaffMapper staffMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public StaffLoginVO getStaffLoginVOByPhone(String phone) {
        return staffMapper.findStaffLoginVOByPhone(phone);
    }

    @Override
    public void insertStaff(StaffDTO staffDTO) {
        //判断是否手机号已存在
        Staff s = staffMapper.findByPhone(staffDTO.getPhone());
        if (s != null){
            throw new PhoneDuplicateException("添加职员失败，手机号已存在");
        }

        Staff staff = new Staff();
        staff.setUsername(staffDTO.getUsername());
        staff.setEmail(staffDTO.getEmail());
        staff.setJobId(staffDTO.getJobId());
        staff.setMaritalStatus(staffDTO.getMaritalStatus());
        staff.setAge(staffDTO.getAge());
        staff.setGender(staffDTO.getGender());
        staff.setEducation(staffDTO.getEducation());
        staff.setPhone(staffDTO.getPhone());
        staff.setAddress(staffDTO.getAddress());
        staff.setDeptId(staffDTO.getDeptId());
        String idNumber = staffDTO.getIdNumber();
        staff.setIdNumber(idNumber);
        String password = idNumber.substring(idNumber.length()-6);
        //使用密码加密器进行加密
        staff.setPassword(PasswordUtils.encode(password));
        LocalDateTime now = LocalDateTime.now();
        staff.setGmtCreate(now);
        staff.setGmtModified(now);
        int rows = staffMapper.insert(staff);
        if (rows != 1){
           throw new InsertException("新增职员失败，服务器忙，请稍后重试");
        }
    }

    @Override
    public void deleteById(Integer id) {
        Staff staff = staffMapper.selectById(id);
        if (staff == null){
            throw new NotFoundException("删除失败，职员员不存在");
        }
        int rows = staffMapper.deleteById(id);
        if (rows != 1){
            throw new DeleteException("删除职员失败，服务器忙，请稍后重试");
        }
    }

    @Override
    public StaffUpdateVO showUpdateStaffById(Integer id) {
        StaffUpdateVO staffUpdateVO = staffMapper.showUpdateById(id);
        return staffUpdateVO;                                      
    }

    @Override
    public void updateStaff(StaffUpdateVO staffUpdateVO) {
        Integer id = staffUpdateVO.getId();
        Staff staff = staffMapper.selectById(id);
        if (staff == null){
            throw new NotFoundException("修改职员失败，职员不存在");
        }
        staff.setUsername(staffUpdateVO.getUsername());
        staff.setPhone(staffUpdateVO.getPhone());
        staff.setGender(staffUpdateVO.getGender());
        staff.setEmail(staffUpdateVO.getEmail());
        staff.setDeptId(staffUpdateVO.getDeptId());
        staff.setJobId(staffUpdateVO.getJobId());
        LocalDateTime now =LocalDateTime.now();
        staff.setGmtModified(now);
        int rows = staffMapper.updateById(staff);
        if (rows != 1){
            throw new UpdateException("修改职员失败，服务器忙");
        }
    }

    @Override
    public List<StaffVO> findList(Integer id) {
        List<StaffVO> list = new ArrayList<>();
        Department department = departmentMapper.selectById(id);
        Integer pId = department.getPId();
        if (pId == 0 ){//总公司
            list = staffMapper.findAll();
        }else if (pId == 1){//分公司
            list = staffMapper.findByPId(id);
        }else {
            list = staffMapper.findByDeptId(id);
        }
        return list;
    }
}
